-- ======================================================================
-- Hell Lua Bus
-- 
-- Copyright 2012-2013Hell-Prototypes
--
-- http://www.hellprototypes.com/
--
-- This is free software, licensed under the terms of the GNU General
-- Public License as published by the Free Software Foundation.
-- ======================================================================
iic = require "mod_fw_iic"

--Reg define
XOUTL         =  0x00
XOUTH         =  0x01
YOUTL         =  0x02
YOUTH         =  0x03
ZOUTL         =  0x04
ZOUTH         =  0x05
XOUT8         =  0x06
YOUT8         =  0x07
ZOUT8         =  0x08
STATUS        =  0x09
DETSRC        =  0x0A
TOUT          =  0x0B
I2CAD         =  0x0D
USRINF        =  0x0E
WHOAMI        =  0x0F
XOFFL         =  0x10
XOFFH         =  0x11
YOFFL         =  0x12
YOFFH         =  0x13
ZOFFL         =  0x14
ZOFFH         =  0x15
MCTL          =  0x16
INTRST        =  0x17
CTL1          =  0x18
CTL2          =  0x19
LDTH          =  0x1A
PDTH          =  0x1B
PW            =  0x1C
LT            =  0x1D
TW            =  0x1E


function mma7455x_startup()
	iic.write_reg(MCTL, 0x4d)       -- 2g measurement mode, data ready signal not output
--                               b01000101
--                                ||||||||
--                                ||||||++- MODE: 00 - Standby
--                                ||||||          01 - Measurement
--                                ||||||          10 - Level Detection
--                                ||||||          11 - Pulse Detection
--                                ||||++--- GLVL: 00 - 8 G
--                                ||||            10 - 4 G 
--                                ||||            01 - 2 G 
--                                |||+----- STON: 0 - Self Test Disabled
--                                ||+------ SPI3W:0 - 3-wire SPI
--                                |+------- DRPD: 1 - Data Ready NOT Output to PIN1
--                                +-------- Reserved: 0

	iic.write_reg(CTL1, 0x80)
--                               b00000000
--                                |||||||+- INTPIN: 0 - INT pins and register bits do not swap
--                                |||||++-- INTREG[1:0]: 00 - INT1 for level detection, INT2 for pulse detection
--                                ||||+---- XDA: 0 - X axis enabled for detection
--                                |||+----- YDA: 0 - Y axis enabled for detection
--                                ||+------ ZDA: 0 - Z axis enabled for detection
--                                |+------- THOPT: 0 - Level detection threshold is absolute value
--                                +-------- DFBW: 0 - Digital filter band width is 62.5Hz

	iic.write_reg(CTL2, 0x00)       --b00000000
--                                |||
--                                ||+- LDPL: 0 - Level detection polarity is positive
--                                |+-- PDPL: 0 - Pulse detection polarity is positive
--                                +--- DRVO: 0 - Standard drive strength on SDA/SDO pin
	iic.write_reg(LDTH, 0x00)       -- Level detection threshold
	iic.write_reg(PDTH, 0x00)       -- Pulse detection threshold
	iic.write_reg(PW, 0x00)         -- Pulse Duration, 0.5mS per count
	iic.write_reg(LT, 0x00)         -- Latency time, max 255mS
	iic.write_reg(TW, 0x00)         -- Time Window for 2nd Pulse, max 255mS
--	MMA7455L_ClearInterrupt;
end
-- -----------------------------------------------------------------------------

	if not iic.device_open() then
		print("iic.device_open fail")
		return
	end
	
	iic.io_init()
	iic.device_addr(0x3A)
	
	mma7455x_startup()
	count = 1000
	last_x = 0
	last_y = 0
	last_z = 0
	x = 0
	y = 0
	z = 0
 
	for i=1, 10 do
		t = iic.read_regs(0, 6)
		if #t > 0 then
			x = t[1] + t[2]*256
			y = t[3] + t[4]*256
			z = t[5] + t[6]*256
			
			if (math.floor(x/10) ~= math.floor(last_x/10)) or (math.floor(y/10) ~= math.floor(last_y/10)) or (math.floor(z/10) ~= math.floor(last_z/10)) then
				print("X = ",  x)
				print("Y = ",  y)
				print("Z = ",  z)
				
				last_x = x
				last_y = y
				last_z = z
			end
		else
			--print("Read regs Error")
		end
		iic.hlb.msdelay(100)
	end

	if not iic.device_close() then
		print("iic.device_close fail")
	end

